#include <vector>
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

const int val[5][5] =
    {
        5, -1, -2, -1, -3,
        -1, 5, -3, -2, -4,
        -2, -3, 5, -2, -2,
        -1, -2, -2, 5, -1,
        -3, -4, -2, -1, -5};

int c2i(char x)
{
    switch (x)
    {
    case 'A':
        return 0;
    case 'C':
        return 1;
    case 'G':
        return 2;
    case 'T':
        return 3;
    default:
        return 4;
    }
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        string s1, s2;
        cin >> n >> s1;
        cin >> m >> s2;
        vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
        for (int i = 0; i < n; i++)
            dp[i + 1][0] = dp[i][0] + val[c2i(s1[i])][4];
        for (int i = 0; i < m; i++)
            dp[0][i + 1] = dp[0][i] + val[c2i(s2[i])][4];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
            {
                dp[i + 1][j + 1] = max(dp[i][j] + val[c2i(s1[i])][c2i(s2[j])],
                               max(dp[i][j + 1] + val[c2i(s1[i])][c2i('-')],
                                   dp[i + 1][j] + val[c2i(s2[j])][c2i('-')]));
            }
        cout << dp[n][m] << endl;
    }
    return 0;
}